package com.boot.security.server.controller.mffl.mffllawyerletter;

import com.boot.security.server.dao.mffl.MfflLawyerletterDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MfflCourierServicesOrder;
import com.boot.security.server.model.mffl.MfflLawyerletter;
import com.boot.security.server.model.mffl.Vo.MfflLawyerletterVo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.impl.mffl.MfflLawyerletter.MfflLawyerletterService;
import com.boot.security.server.service.impl.mffl.WXPay.WXPayService;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.boot.security.server.service.impl.mffl.MfflUser.mfflUserService;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 律师函客户端
 */
@Api(tags = "律师函(用户端)")
@RestController
@RequestMapping("/client/mfflLawyerletters")
public class Clinet_MfflLawyerletterController {

    @Autowired
    private MfflLawyerletterDao mfflLawyerletterDao;

    @Autowired
    MfflLawyerletterService mffllawyerletterservice;

    @Autowired
    mfflUserService mffluserservice;

    //微信支付
    @Autowired
    WXPayService wxPayService;

    @PostMapping
    @ApiOperation(value = "发布律师函(返回商户订单号----等待前端发起支付)")
    public ReturnBean save(@ApiParam(value = "律师函类型id", name = "typeId", required = true) @RequestParam Integer typeId,
                           @ApiParam(value = "称呼", name = "Realname", required = true) @RequestParam String Realname,
                           @ApiParam(value = "联系方式", name = "number", required = true) @RequestParam String number,
                           @ApiParam(value = "事件描述", name = "describe", required = true) @RequestParam String describe,
                           @ApiParam(value = "图片地址(按照分号分割)", name = "imageUrl", required = false) @RequestParam(required = false) String imageUrl,
                           @ApiParam(value = "函件双方（我方）", name = "We", required = true) @RequestParam String We,
                           @ApiParam(value = "函件双方（对方）", name = "otherParty", required = true) @RequestParam String otherParty,
                           @ApiParam(value = "备注", name = "content", required = false) @RequestParam(required = false) String content,
                           @ApiParam(value = "所选律师id", name = "serviceLawyerId", required = true) @RequestParam Integer serviceLawyerId) throws Exception {
        if (typeId == null) {
            return new ReturnBean("201", "typeId不能为空");
        }
        if (Realname == null) {
            return new ReturnBean("201", "Realname不能为空");
        }
        if (number == null) {
            return new ReturnBean("201", "number不能为空");
        }
        if (describe == null) {
            return new ReturnBean("201", "describe不能为空");
        }
        if (We == null) {
            return new ReturnBean("201", "We不能为空");
        }
        if (otherParty == null) {
            return new ReturnBean("201", "otherParty不能为空");
        }
        if (serviceLawyerId == null) {
            return new ReturnBean("201", "serviceLawyerId不能为空");
        }
        //返回结果
        return mffllawyerletterservice.save(typeId, Realname, number, describe, imageUrl, We, otherParty, content, serviceLawyerId);
    }


    @PostMapping(value = "pay")
    @ApiOperation(value = "支付")
    public ReturnBean pay(@ApiParam(name = "paymerchantnumber", value = "支付订单号", required = true) @RequestParam String paymerchantnumber) {
        if (paymerchantnumber == null) {
            return new ReturnBean("paymerchantnumber不能为空", "201");
        }
        ReturnBean returnBean = mffllawyerletterservice.LawyerletterServicePay(paymerchantnumber, "/client/mfflLawyerletters/wechatpaycallback");
        return returnBean;
    }


    @ApiIgnore
    @RequestMapping(value = "wechatpaycallback")
    @ResponseBody
    @Transactional
    public String WechatPayCallback(HttpServletRequest request) throws Exception {

        //处理微信回调数据
        Map<String, String> returnmap = wxPayService.WechatPayCallback(request);


        //修改订单信息
        boolean b = mffllawyerletterservice.WechatPayCallback(returnmap);

        //微信回调数据
        return wxPayService.WechatPayCallbackReturnData(b);
    }


    @PostMapping(value = "writeemailinfo")
    @ApiOperation(value = "填写邮件信息")
    public ReturnBean writeEmailInfo(@ApiParam(name = "id", value = "订单id", required = true) @RequestParam Integer id,
                                     @ApiParam(name = "addRessee", value = "收件人", required = true) @RequestParam String addRessee,
                                     @ApiParam(name = "recipientPhoneNumber", value = "联系电话", required = true) @RequestParam String recipientPhoneNumber,
                                     @ApiParam(name = "address", value = "收件地址", required = true) @RequestParam String address) {
        if (id == null) {
            return new ReturnBean("201", "订单id不能为空");
        }
        if (addRessee == null) {
            return new ReturnBean("201", "收件人不能为空");
        }
        if (recipientPhoneNumber == null) {
            return new ReturnBean("201", "联系电话不能为空");
        }
        if (address == null) {
            return new ReturnBean("201", "收件地址不能为空");
        }
        return mffllawyerletterservice.writeEmailInfo(id, addRessee, recipientPhoneNumber, address);
    }

    @GetMapping(value = "Phone")
    @ApiOperation("用户拨打律师电话")
    public ReturnBean Phone(@ApiParam(name = "id", value = "订单id", required = true) @RequestParam Integer id) {
        if (id == null) {
            return new ReturnBean("201", "订单id不能为空");
        }
        return mffllawyerletterservice.Phone(0, id);
    }


    @PostMapping(value = "showCourier")
    @ApiOperation("查看律师函快递快递信息")
    public ReturnBean<MfflCourierServicesOrder> showCourier(@ApiParam(name = "id", value = "律师函id", required = true) @RequestParam Integer id) {
        if (id == null) {
            return new ReturnBean("201", "律师函id不能为空");
        }
        return mffllawyerletterservice.showCourier(id);
    }


    @GetMapping(value = "ConfirmCompletion")
    @ApiOperation(value = "用户确认完成")
    public ReturnBean ConfirmCompletion(@ApiParam(name = "lawyerletterId", value = "律师函id", required = true) @RequestParam Integer lawyerletterId,
                                        @ApiParam(name = "comment", value = "评论内容", required = true) @RequestParam String comment,
                                        @ApiParam(name = "rank", value = "评论级别 1好评 2中评 3差评", required = true) @RequestParam Integer rank) {
        if (lawyerletterId == null) {
            return new ReturnBean("201", "律师函id不能为空");
        }
        if (StringUtils.isEmpty(comment)) {
            return new ReturnBean("201", "评论内容不能为空");
        }
        if (rank == null) {
            return new ReturnBean("201", "评论级别不能为空");
        }
        if (rank < 0 && rank > 3) {
            return new ReturnBean("201", "评论数字不得小于0,或大于3");
        }

        ReturnBean returnbean = null;
        try {
            returnbean = mffllawyerletterservice.comment(comment, rank, lawyerletterId);
            returnbean = mffllawyerletterservice.ConfirmCompletion(lawyerletterId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return returnbean;
        }
    }


    @GetMapping(value = "/{id}")
    @ApiOperation(value = "按id获取律师函")
    public ReturnBean<MfflLawyerletterVo> get(@ApiParam(name = "id", value = "律师函id", required = true) @PathVariable Integer id) {
        if (id == null) {
            return new ReturnBean("201", "律师函id不能为空");
        }
        return mffllawyerletterservice.getIdlawyerletter(id);
    }


    @GetMapping
    @ApiOperation(value = "列表")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int")})
    public ReturnBean<MfflLawyerletter> list(PageTableRequest request) {
        if (request.getOffset() == null) {
            return new ReturnBean("201", "offset不能为空");
        }
        if (request.getLimit() == null) {
            return new ReturnBean("201", "limit不能为空");
        }

        //查询当前用户的
        SysUser sysUser = UserUtil.getLoginUser();
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", sysUser.getId());
        map.put("deleteState", 1);
        request.setParams(map);

        PageTableResponse handle = new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflLawyerletterDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflLawyerletter> list(PageTableRequest request) {
                return mfflLawyerletterDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);

        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(handle);
        return returnBean;
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "撤销发布")
    public ReturnBean delete(@ApiParam(name = "id", value = "律师函id", required = true) @PathVariable Long id) {
        if (id == null) {
            return new ReturnBean("201", "律师函id不能为空");
        }
        ReturnBean returnBean = null;
        try {
            returnBean = mffllawyerletterservice.cancelLawyerletter(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return returnBean;
        }
    }


    @GetMapping("refund")
    @ApiOperation(value = "申请退款")
    public ReturnBean refund(@ApiParam(name = "id", value = "律师函id", required = true) @RequestParam Long id,
                             @ApiParam(name = "refundreason", value = "退款原因", required = true) @RequestParam String refundreason) throws Exception {
        if (id == null) {
            return new ReturnBean("201", "律师函id不能为空");
        }
        if (StringUtils.isEmpty(refundreason)) {
            return new ReturnBean("201", "退款原因不能为空");
        }
        return mffllawyerletterservice.refund(id, refundreason);
    }
   /* @GetMapping("getsrvicelaywer")
    @ApiOperation("获取律师函服务的律师")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int")})
    public ReturnBean getServiceLaywer(PageTableRequest reques) {
        return mffluserservice.getServiceLawyer(reques, 3);
    }

*/

    @GetMapping("cancelRefund")
    @ApiOperation(value = "取消申请退款")
    public ReturnBean cancelRefund(@ApiParam(name = "id", value = "律师函id", required = true) @RequestParam Long id) {
        if (id == null) {
            return new ReturnBean("201", "律师函id不能为空");
        }
        try {
            return mffllawyerletterservice.cancelRefund(id);
        } catch (Exception e) {
            e.printStackTrace();
            ReturnBean returnBean = new ReturnBean();
            returnBean.setReturnBean("105");
            return returnBean;
        }
    }

    @PostMapping("orderquery")
    @ApiOperation(value = "查询订单状态(不信任回调函数，)")
    public ReturnBean orderquery(String payMerchantNumber) throws Exception {
        return mffllawyerletterservice.orderquery(payMerchantNumber);
    }

   /* @GetMapping("text")
    public void text(){
        WXPayService x = new  WXPayService();
        x.refund();
    }*/

  /*  @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public MfflLawyerletter get(@PathVariable Long id) {
        return mfflLawyerletterDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public MfflLawyerletter update(@RequestBody MfflLawyerletter mfflLawyerletter) {
        mfflLawyerletterDao.update(mfflLawyerletter);

        return mfflLawyerletter;
    }
    */
}
